<template>
  <div>
    <!-- 搜索框 -->
    <el-row id="" style="padding: 10px 0">
      <el-col :span="18">
        <el-input v-model="names" placeholder="角色名称"></el-input>
      </el-col>
      <el-col :span="6" style="padding-left: 15px">
        <el-button type="primary" @click="getuserlist(1)">搜索</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-col>
    </el-row>
    <!-- 功能按钮 -->
    <el-button
      type="primary"
      v-for="i in $store.state.user.menulist[this.$route.path]"
      :key="i.id"
      @click="btn(i.code)"
      >{{ i.name }}</el-button
    >
    <!--  {{ this.$store.state.user.menulist[this.$route.path] }} -->
    <!-- table表格 -->
    <div v-if="tableData" style="padding: 20px 0">
      <el-table
        :data="tableData"
        style="width: 100%"
        height="300"
        @cell-click="celltable"
        :highlight-current-row="true"
      >
        <el-table-column label="角色名称" prop="name"> </el-table-column>
        <el-table-column label="部门名称" prop="deptName"> </el-table-column>
        <el-table-column label="id名" prop="id"> </el-table-column>
      </el-table>
    </div>
    <!-- <div>
      <el-pagination
        :background="true"
        layout="prev, pager, next"
        :total="total"
        :page-size="5"
        @current-change="pagetype"
      >
      </el-pagination>
    </div> -->
    <!-- 功能组件 -->
    <!-- 添加角色 -->
    <role-add
      :type="tabletype.roleAdd"
      :fun="cleartype"
      :tname="'roleAdd'"
    ></role-add>
    <!-- 修改角色 -->
    <role-edit
      :type="tabletype.roleEdit"
      :fun="cleartype"
      :tname="'roleEdit'"
      :tablerow="tablerow"
    ></role-edit>
    <!-- 删除角色 -->
    <role-delete
      :type="tabletype.roleDelete"
      :fun="cleartype"
      :tname="'roleDelete'"
      :tablerow="tablerow"
    ></role-delete>
    <!-- 配置角色权限 -->
    <role-set-authority
      :type="tabletype.roleSetAuthority"
      :fun="cleartype"
      :tname="'roleSetAuthority'"
      :tablerow="tablerow"
    ></role-set-authority>
  </div>
</template>

<script>
// 引入网络请求的组价
import { http, getrolelist } from "../../api/api";
// 引入添加功能的组件
import RoleAdd from "./RoleAdd"; // 添加角色
import RoleEdit from "./RoleEdit"; // 修改角色
import RoleDelete from "./RoleDelete"; //删除角色
import RoleSetAuthority from "./RoleSetAuthority"; //角色分配
export default {
  data() {
    return {
      tableData: "", //用户列表数据
      names: "", //搜索的名字
      tabletype: {
        //功能对话框状态
        roleAdd: false, //添加角色
        roleEdit: false, //修改角色
        roleDelete: false, //删除角色
        roleSetAuthority: false, //分配角色
      },
      // 单元格选择的对象
      tablerow: "",
    };
  },
  components: {
    RoleAdd,
    RoleEdit,
    RoleDelete,
    RoleSetAuthority,
  },
  mounted() {
    var json = this.$store.state.user.menulist[this.$route.path],
      arr1 = json.slice(0, 3),
      arr2 = json.slice(-1);
    var test = arr1.concat(arr2);
    this.$store.state.user.menulist[this.$route.path] = test;

    this.getuserlist();
  },
  methods: {
    // 将tabletype中的地址值变成false状态，关闭模态框
    cleartype(type) {
      this.tabletype[type] = false;
      this.tablerow = "";
      this.getuserlist();
    },
    // 选择单元格触发的事件
    celltable(row) {
      this.tablerow = row;
      console.log(row);
    },
    // 统一的功能按钮
    btn(a) {
      console.log(a);
      if (a == "roleAdd") {
        this.tabletype[a] = true;
      } else {
        if (this.tablerow == "") {
          this.$message.error("请选择要操作的角色");
        } else {
          this.tabletype[a] = true;
        }
      }
    },
    // 重置功能
    reset() {
      this.names = "";
      this.getuserlist(1);
    },
    // 网络请求列表数量
    getuserlist() {
       const loading = this.$loading({
        lock: true,
        text: "Loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.$http
        .get(http + getrolelist, {
          params: {
            name: this.names,
          },
        })
        .then(
          (data) => {
            console.log(data.data);
            if (data.data.msg == "成功") {
              // 赋值角色列表
              this.tableData = data.data.data;
              // 关闭动画
              setTimeout(() => {
                loading.close();
              }, 1000);
            } else {
              this.$message.error(data.data.message);
            }
          },
          (err) => {
            this.$message.error(err.data.message);
          }
        );
    },
  },
};
</script>

<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>